import { request } from '../../utils/request'
import { debounce } from '../../utils/tool'
let timer = 0
let interval = 300
let that
Page({

  /**
   * 页面的初始数据
   */
  data: {
    defaultSearch:'',
    searchInput:'',
    keyWordsList:[],
    isSearch:false,
    isSearchResult:false,
    searchHistory:[]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    that = this
    that.getInitData()
  },
  getInitData: async () => {
    const { data:{showKeyword:defaultSearch} } = await request('search/default')
    const { data:hotList } = await request('search/hot/detail')
    that.setData({
      defaultSearch,
      hotList
    })
  },
  cancelSearch: () => {
    that.getSearchSuggest('')
    that.setData({
      searchInput:''
    })
  },
  handleSearchInput: debounce(function(e){
    const {value:searchInput} = e.detail
    that.setData({
      searchInput
    })
    that.getSearchSuggest(searchInput)
  }, interval),  
  // handleSearchInput: e => {

 	// 	const {value:searchInput} = e.detail
  //   that.setData({
  //     searchInput
  //   })
  //   that.
  //   clearTimeout(timer) //清除定时器
  //   timer = setTimeout(()=>{     
  //     that.getSearchSuggest(searchInput)
  //   }, interval)
  // },
  //获取搜索建议, 点击搜索建议的某个关键词getSongByKeyword函数跳到该搜索结果页面
  getSearchSuggest: async (searchInput, type) => {
    if(searchInput === '') return that.setData({
      keyWordsList:[],
      isSearch:false,
      isSearchResult:false
    })
    type = type || 'mobile'
    let { result:{allMatch:keyWordsList}} = await request('search/suggest', {
      keywords: searchInput,
      type
    })
    let isSearchResult = true
    if(typeof keyWordsList === 'undefined'){
      keyWordsList = []
      isSearchResult = false
    }
    that.setData({
      keyWordsList,
      isSearch:true,
      isSearchResult
    })
  },
  chooseSearchSuggest: (e) => {
    const { target:{ dataset:{ keyword } } } = e
    that.getSearchResultByKeyword(keyword)
  },
/*   
根据搜索关键词和搜索类型返回搜索结果
  type:{
    1: 单曲, 
    10: 专辑, 
    100: 歌手, 
    1000: 歌单, 
    1002: 用户, 
    1004: MV, 
    1006: 歌词, 
    1009: 电台, 
    1014: 视频, 
    1018:综合, 
    2000:声音
  } 
*/
  getSearchResultByKeyword: async (keywords, type) => {
    const { searchHistory } = that.data
    //如果已经存在该关键词, 需要删除后重新unshift, 这样才能将其顺序到最新历史记录
    const index = searchHistory.indexOf(keywords)
    //index存在, 那么先删除该index
    if(index !== -1) {
      searchHistory.splice(index, 1)
    }
    //不管index存在不存在, 都要unshift最新的index
    searchHistory.unshift(keywords)
    type = type || '1'
    let { result:{ songs: songList }} = await request('cloudsearch', {keywords, type})
    that.setData({
      songList,
      searchHistory
    })
  },
  deleteHistory: () => {
    wx.showModal({
      title: '提示',
      content: '确定删除历史搜索记录吗？',
      success (res) {
        if (res.confirm) {
          that.setData({
            searchHistory:[]
          })
        } 
        else if (res.cancel) {
          return
        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})